package pub.guoxin.oops.manager.handler.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import pub.guoxin.oops.common.sms.enums.SmsTemplate;
import pub.guoxin.oops.enums.ComparisonOperator;
import pub.guoxin.oops.manager.handler.AbstractOneBoundaryAlarmInfoHandler;
import pub.guoxin.oops.manager.vo.AlarmCacheVo;
import pub.guoxin.oops.model.AlarmInfo;
import pub.guoxin.oops.model.Communication;
import pub.guoxin.oops.model.CommunicationData;
import pub.guoxin.oops.model.DeviceDataInfo;

import java.util.List;

import static pub.guoxin.oops.manager.handler.AlarmFieldNameConstants.*;

@Slf4j
@Service(DEVICE_BATTERY + AlarmInfoHandler)
public class DeviceBatteryAlarmInfoHandlerImpl extends AbstractOneBoundaryAlarmInfoHandler {

    /**
     * @param communicationData 被处理数据
     * @param alarmInfos  报警信息
     * @return
     */
    @Override
    public List<AlarmInfo> handleRequest(CommunicationData communicationData, List<AlarmInfo> alarmInfos) {
        Communication communication = communicationData.getCommunication();
        List<DeviceDataInfo> deviceDataInfos = communicationData.getDeviceDataInfos();

        String communicationSn = communication.getSn();
        deviceDataInfos.forEach(deviceDataInfo -> {
            String deviceSn = deviceDataInfo.getSn();
            Float batteryLevel = deviceDataInfo.getBatteryLevel();
            AlarmCacheVo lessCache = getAlarmInfo(communicationSn, deviceSn, ComparisonOperator.LESS_THAN, DEVICE_BATTERY);
            ComparisonOperator comparisonOperator = judgmentBoundaryValue(batteryLevel, DEVICE_BATTERY);
            switch (comparisonOperator) {
                case NORMAL:
                    dataNormal(lessCache, communicationSn, deviceSn, ComparisonOperator.LESS_THAN, DEVICE_BATTERY);
                    break;
                case GREATER_THAN:
                    log.error("逻辑错误，通讯模块电量没有大于");
                    break;
                case LESS_THAN:
                    deviceSnGreaterThanOrLessThan(lessCache, communicationSn, deviceSn, batteryLevel, COMMUNICATION_BATTERY, SmsTemplate.DEVICE_BATTERY_LEVEL, ComparisonOperator.LESS_THAN);
                    break;
            }

        });

        if (this.isNextHandler()) {
            this.getSuccessor().handleRequest(communicationData, alarmInfos);
        }
        return alarmInfos;
    }

}
